package com.xm.xmapp;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.fragment.PileOrderFragment;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.ZFBInterFace;
import com.xm.xmapp.request.bean.Add_pay_orderRequest;
import com.xm.xmapp.request.bean.Debit_moneyRequest;
import com.xm.xmapp.request.bean.Get_ali_pay_signRequest;
import com.xm.xmapp.request.bean.Get_weixin_pay_signRequest;
import com.xm.xmapp.response.bean.ResponseALIpaysign;
import com.xm.xmapp.response.bean.ResponseAddPayOrder;
import com.xm.xmapp.response.bean.ResponseDebitMoney;
import com.xm.xmapp.response.bean.ResponsePileOrderDetail;
import com.xm.xmapp.response.bean.ResponseUserMoney;
import com.xm.xmapp.response.bean.ResponseWXpaysign;
import com.xm.xmapp.tools.PayUitl;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;

public class PileOrderPayActivity extends BaseActivity implements OnClickListener {
	private TextView pay;
	private RelativeLayout rel0, rel1, rel2;
	private int flag;// 0是钱包1是支付宝2是微信
	private ImageView img0, img1, img2;
	private TextView money, orderMoney, tv_money;
	private int payType;
	private ResponseUserMoney mUserMoneyBean;// 钱包信息
	private ResponsePileOrderDetail pileOrderDetailBean;
	private boolean isSuppertWattlePay;// 是否支持钱包支付

	// 支付完订单后调用
	@Override
	public Object dataChang(int flag, Object data) {
		switch (flag) {
		case Type.A_PileOrderPay.DEFAULT:
			finish();
			break;

		default:
			break;
		}	
		return super.dataChang(flag, data);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pile_pay_order);
	}

	@Override
	public void initView() {
		pay = (TextView) findViewById(R.id.pay);
		rel0 = (RelativeLayout) findViewById(R.id.rel0);
		rel1 = (RelativeLayout) findViewById(R.id.rel1);
		rel2 = (RelativeLayout) findViewById(R.id.rel2);
		img0 = (ImageView) findViewById(R.id.img0);
		img1 = (ImageView) findViewById(R.id.img1);
		img2 = (ImageView) findViewById(R.id.img2);

		money = (TextView) findViewById(R.id.money);
		tv_money = (TextView) findViewById(R.id.tv_money);
		orderMoney = (TextView) findViewById(R.id.orderMoney);
	}

	@Override
	public void initData() {

		pileOrderDetailBean = (ResponsePileOrderDetail) getIntent().getSerializableExtra("pileOrderDetailBean");
		mUserMoneyBean = (ResponseUserMoney) getIntent().getSerializableExtra("mUserMoneyBean");
		setMyTitle("订单支付");
		money.setText(pileOrderDetailBean.charging_total_money + "元");

		orderMoney.setText("电桩订单金额");
		rel0.setVisibility(View.VISIBLE);
		tv_money.setText(StringTools.getString(mUserMoneyBean.user_money, "0.00") + "元");
		if (mUserMoneyBean.user_money >= Double.parseDouble(pileOrderDetailBean.pay_money)) {
			isSuppertWattlePay = true;
		} else {
			isSuppertWattlePay = false;
		}
		setMorenPayType();

	}

	/**
	 * 设置默认支付方式
	 */
	private void setMorenPayType() {
		if (isSuppertWattlePay) {
			payType = 5;
			flag = 0;
			setImg();
		} else {
			flag = 1;
			payType = 2;// 6=微信app支付 2=支付宝支付
			setImg();
		}
	}

	@Override
	public void initEvent() {
		pay.setOnClickListener(this);
		rel0.setOnClickListener(this);
		rel1.setOnClickListener(this);
		rel2.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pay:
			if (flag == 0) {
				payPileOrder();
			} else {
				add_pay_orderRequest();
			}
			break;
		case R.id.rel0:
			if (isSuppertWattlePay) {
				payType = 5;
				flag = 0;
				setImg();
			} else {
				Tools.showMessage("钱包余额不足，请选择其他支付方式");
			}

			break;
		case R.id.rel1:
			payType = 2;
			flag = 1;
			setImg();
			break;

		case R.id.rel2:
			payType = 6;
			flag = 2;
			setImg();
			break;
		default:
			break;
		}
	}

	private void add_pay_orderRequest() {
		Add_pay_orderRequest requestBean = new Add_pay_orderRequest();
		requestBean.type = 14;// 1=钱包充值，2=违章押金，13=车辆押金，14=充电桩单
		requestBean.price = pileOrderDetailBean.charging_total_money;
		requestBean.payType = payType;// 6=微信app支付 2=支付宝支付
		requestBean.order_id = pileOrderDetailBean.charging_pile_order_id;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				ResponseAddPayOrder base = JSON.parseObject(response, ResponseAddPayOrder.class);
				if (payType == 2) {
					get_ali_pay_signRequest(base.pay_order_number);
				} else {
					get_weixin_pay_signRequest(base.pay_order_number);
				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				if (promptDialog != null)
					promptDialog.dismiss();
				pay.setClickable(true);
			}
		});
	}

	@Override
	public void onPause() {
		super.onPause();
		if (promptDialog != null) {
			promptDialog.dismiss();
		}
	}

	private void payPileOrder() {
		Debit_moneyRequest requestBean = new Debit_moneyRequest();
		requestBean.charging_pile_order_id = pileOrderDetailBean.charging_pile_order_id;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				ResponseDebitMoney responsebean = JSON.parseObject(response, ResponseDebitMoney.class);
				switch (responsebean.Status) {
				case 1:
					MyAppcation.getMyAppcation().notifyDataSetChanged(PileOrderFragment.class, 0, 0);
					pay.setVisibility(View.GONE);
					PayUitl.getInstence().afterPayMoney();
					break;
				case 4:
					Tools.showMessage("钱包余额不足，请选择其他支付方式");
					isSuppertWattlePay = false;
					setMorenPayType();
					break;
				default:
					Tools.showMessage("此订单不支持钱包支付");
					isSuppertWattlePay = false;
					break;
				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	/**
	 * 获取支付宝支付参数
	 * 
	 * @param pay_order_number
	 */
	private void get_ali_pay_signRequest(String pay_order_number) {

		Get_ali_pay_signRequest requestBean = new Get_ali_pay_signRequest();
		requestBean.pay_order_number = pay_order_number;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				pay.setClickable(true);
				promptDialog.dismiss();
				ResponseALIpaysign base = JSON.parseObject(response, ResponseALIpaysign.class);
				PayUitl.getInstence().zfb(PileOrderPayActivity.this, new ZFBInterFace() {

					@Override
					public void success() {
						PayUitl.getInstence().afterPayMoney();
					}
				}, base.sign_string);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				pay.setClickable(true);
				promptDialog.dismiss();
			}
		});
	}

	/**
	 * 微信支付
	 * 
	 * @param pay_order_number
	 */
	protected void get_weixin_pay_signRequest(String pay_order_number) {
		Get_weixin_pay_signRequest requestBean = new Get_weixin_pay_signRequest();
		requestBean.pay_order_number = pay_order_number;
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				pay.setClickable(true);
				JSONObject obj = JSONObject.parseObject(response);
				ResponseWXpaysign base = JSON.parseObject(response, ResponseWXpaysign.class);
				base.package_str = obj.getString("package");
				PayUitl.getInstence().genWxPay(PileOrderPayActivity.this, base, promptDialog);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				pay.setClickable(true);
				promptDialog.dismiss();
			}
		});
	}

	private void setImg() {

		if (flag == 1) {
			img0.setImageResource(R.mipmap.icon_xuanze_dis);
			img1.setImageResource(R.mipmap.icon_xuanze_nor);
			img2.setImageResource(R.mipmap.icon_xuanze_dis);
		} else if (flag == 2) {
			img0.setImageResource(R.mipmap.icon_xuanze_dis);
			img1.setImageResource(R.mipmap.icon_xuanze_dis);
			img2.setImageResource(R.mipmap.icon_xuanze_nor);
		} else {
			img0.setImageResource(R.mipmap.icon_xuanze_nor);
			img1.setImageResource(R.mipmap.icon_xuanze_dis);
			img2.setImageResource(R.mipmap.icon_xuanze_dis);
		}
	}

}
